package com.example.bookseckill.controller;


import com.example.bookseckill.entity.SeckillBook;
import com.example.bookseckill.entity.to.SeckillSkuRedisTo;
import com.example.bookseckill.service.SeckillService;
import com.example.bookseckill.service.SeckillSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 秒杀信息查询
 */
@RestController
@RequestMapping("/seckill")
public class SeckillBookController {
    @Resource
    private SeckillSessionService seckillSessionService;
    @Autowired
    private SeckillService seckillService;
    /**
     * 查询最近三天需要参加秒杀商品的信息
     *
     * @return
     */
    @GetMapping(value = "/Lates3DaySession")
    public List<SeckillBook> getLates3DaySession() {

        List<SeckillBook> seckillSessionEntities = seckillSessionService.getLates3DaySession();

        return seckillSessionEntities;
    }

    /**
     * 当前时间可以参与秒杀的商品信息
     */
    @GetMapping("/getCurrentSeckillSkus")
    public List<SeckillSkuRedisTo> getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return vos;
    }

    /**
     * 商品进行秒杀
     * @param killId    具体单个书本信息
     * @param key      商品的随机码
     * @param num
     * @param model
     * @return
     */

    @GetMapping(value = "/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        String orderSn = null;
        try {
            //1.判断是否登录
            orderSn = seckillService.kill(killId,key,num);
            model.addAttribute("orderSn",orderSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

}
